#include<bits/stdc++.h>
using namespace std;
namespace my_std{
	#define ll long long
	#define bl bool
	ll my_pow(ll a,ll b,ll mod){
		ll res=1;
		if(!b) return 1;
		while(b){
			if(b&1) res=(res*a)%mod;
			a=(a*a)%mod;
			b>>=1;
		}
		return res;
	}
	ll qpow(ll a,ll b){
		ll res=1;
		if(!b) return 1;
		while(b){
			if(b&1) res*=a;
			a*=a;
			b>>=1;
		}
		return res;
	}
	#define db double
	#define pf printf
	#define pc putchar
	#define fr(i,x,y) for(register ll i=(x);i<=(y);i++)
	#define pfr(i,x,y) for(register ll i=(x);i>=(y);i--)
	#define go(u) for(ll i=head[u];i;i=e[i].nxt)
	#define enter pc('\n')
	#define space pc(' ')
	#define fir first
	#define sec second
	#define MP make_pair
	#define il inline
	#define inf 8e18
	#define random(x) rand()*rand()%(x)
	#define inv(a,mod) my_pow((a),(mod-2),(mod))
	il ll read(){
		ll sum=0,f=1;
		char ch=0;
		while(!isdigit(ch)){
			if(ch=='-') f=-1;
			ch=getchar();
		}
		while(isdigit(ch)){
			sum=sum*10+(ch^48);
			ch=getchar();
		}
		return sum*f;
	}
	il void write(ll x){
		if(x<0){
			x=-x;
			pc('-');
		}
		if(x>9) write(x/10);
		pc(x%10+'0');
	}
	il void writeln(ll x){
		write(x);
		enter;
	}
	il void writesp(ll x){
		write(x);
		space;
	}
}
using namespace my_std;
#define mod 1000000007
ll n,a[1010],b[1010],mp1[1010],mp2[1010],f[1010][3],g[1010][3],ans=0;
int main(){
	freopen("zha.in","r",stdin);
	freopen("zha.out","w",stdout);
	n=read();
	fr(i,1,n) a[i]=read();
	fr(i,1,n) b[i]=read();
	fr(i,1,n) mp1[a[i]]=mp2[b[i]]=i;
	f[0][0]=1;
	fr(k,0,2*n-3){
		fr(i,0,n-1) fr(j,0,2) g[i][j]=f[i][j];
		fr(i,0,n-1) fr(j,0,2) f[i][j]=0;
		fr(i,0,min(n-1,k)){
			ll i1=k-(mp2[a[i+1]]-1),i2=mp1[b[k-i+1]]-1;
			if(g[i][0]){
				if(a[i+1]==b[k-i+1]&&i<(n-1)&&(k-i)<(n-1)) f[i][1]=(f[i][1]+g[i][0])%mod;
				else{
					if(i<(n-1)) f[i+1][0]=(f[i+1][0]+g[i][0])%mod;
					if((k-i)<(n-1)) f[i][0]=(f[i][0]+g[i][0])%mod;
				}
			}
			if(g[i][1]&&i1>i){
				if((k-i)<(n-1)&&i1<(n-1)) f[i][1]=(f[i][1]+g[i][1])%mod;
				else{
					if((k-i)<(n-1)) f[i][0]=(f[i][0]+g[i][1])%mod;
					if(i1<(n-1)) f[i1+1][0]=(f[i1+1][0]+g[i][1])%mod;
				}
				if(b[k-i+1]==a[i1+1]&&i<(n-1)&&(k-i1)<(n-1)){
					if((i+1)==i1) f[i+1][0]=(f[i+1][0]+g[i][1])%mod;
					else if(a[i+2]==b[k-i1+2]) f[i+1][1]=(f[i+1][1]+g[i][1])%mod;
					else f[i+1][2]=(f[i+1][2]+g[i][1])%mod;
				}
				else{
					if(i<(n-1)) f[i+1][0]=(f[i+1][0]+g[i][1])%mod;
					if((k-i1)<(n-1)) f[i1][0]=(f[i1][0]+g[i][1])%mod;
				}
			}
			if(g[i][2]&&i2>i&&i2!=i1){
				if(i<(n-1)&&(k-i2)<(n-1)){
					if((i+1)==i2) f[i+1][0]=(f[i+1][0]+g[i][2])%mod;
					else if(a[i+2]==b[k-i2+2]) f[i+1][1]=(f[i+1][1]+g[i][2])%mod;
					else f[i+1][2]=(f[i+1][2]+g[i][2])%mod;
				}
				else{
					if(i<(n-1)) f[i+1][0]=(f[i+1][0]+g[i][2])%mod;
					if((k-i2)<(n-1)) f[i2][0]=(f[i2][0]+g[i][2])%mod;
				}
				if((k-i)<(n-1)) f[i][0]=(f[i][0]+g[i][2])%mod;
				if(i2<(n-1)) f[i2+1][0]=(f[i2+1][0]+g[i][2])%mod;
			}
		}
		//~ fr(i,0,min(n-1,k+1)) pf("%lld %lld : %lld %lld %lld\n",i,k+1-i,f[i][0],f[i][1],f[i][2]);
	}
	write(f[n-1][0]);
}
/*
3
1 2 3
2 3 1
ans:
5
*/
